/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.share.mybatis.mapper;

import com.share.common.entity.User;
import com.share.common.repository.UserRepository;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

public interface MybatisUserRepository extends UserRepository {
    @Override
    @Update("CREATE TABLE IF NOT EXISTS t_user (user_id INT NOT NULL AUTO_INCREMENT, user_name VARCHAR(200), user_name_plain VARCHAR(200), pwd VARCHAR(200), assisted_query_pwd VARCHAR(200), PRIMARY KEY (user_id));")
    void createTableIfNotExists() throws SQLException;

    @Override
    @Update("drop TABLE t_user;")
    void dropTable() throws SQLException;

    @Override
    @Update("truncate table t_user")
    void truncateTable() throws SQLException;

    @Override
    @Insert("INSERT INTO t_user (user_id, user_name, pwd) VALUES (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{pwd,jdbcType=VARCHAR})")
    Long insert(User entity) throws SQLException;

    @Override
    @Delete("DELETE FROM t_user WHERE user_id = #{userId,jdbcType=INTEGER};")
    void delete(@Param("userId") Long primaryKey) throws SQLException;

    @Override
    @Results(id = "userRepository", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "pwd", column = "pwd"),
            @Result(property = "assistedQueryPwd", column = "assisted_query_pwd")
    })
    @Select("SELECT * FROM t_user;")
    List<User> selectAll() throws SQLException;
}
